package main.java.day01;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @Author:wsl
 * @Data:2023/02/14/16:50
 * @Description:
 */
public class Create {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("").setAppName("");
        JavaSparkContext sc = new JavaSparkContext(conf);

        List<String> result = sc.parallelize(Arrays.asList("hello", "spark")).collect();
        for (String s : result) {
            System.out.println(s);
        }

        JavaRDD<String> lineRdd = sc.textFile("");

        //map
        JavaRDD<String> mapRdd = lineRdd.map(s -> s + "---");
        JavaRDD<String> mapRdd2 = lineRdd.map(new Function<String, String>() {
            @Override
            public String call(String v1) throws Exception {
                return v1 + "---";
            }
        });

        mapRdd.collect().forEach(a -> System.out.println(a));

        //flatmap
        JavaRDD<String> flatMap = lineRdd.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public Iterator<String> call(String s) throws Exception {
                String[] strings = s.split(" ");
                return Arrays.asList(strings).iterator();
            }
        });


        sc.stop();
    }
}
